mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 10:13:52 -05:00
Avoid a naive datetime.now() in switchbot_cloud (#178484)
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
"""Support for the Switchbot Image."""
|
||||
|
||||
import datetime
|
||||
from typing import override
|
||||
|
||||
from switchbot_api import Device, Remote, SwitchBotAPI
|
||||
@@ -9,6 +8,7 @@ from switchbot_api.utils import get_file_stream_from_cloud
|
||||
from homeassistant.components.image import ImageEntity
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import SwitchbotCloudConfigEntry, SwitchBotCoordinator
|
||||
from .entity import SwitchBotCloudEntity
|
||||
@@ -60,7 +60,7 @@ class SwitchBotCloudImage(SwitchBotCloudEntity, ImageEntity):
|
||||
"""Set attributes from coordinator data."""
|
||||
if self.coordinator.data is None:
|
||||
return
|
||||
self._attr_image_last_updated = datetime.datetime.now() # pylint: disable=home-assistant-enforce-naive-now
|
||||
self._attr_image_last_updated = dt_util.utcnow()
|
||||
self._attr_image_url = self.coordinator.data.get("imageUrl")
|
||||
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ from homeassistant.components.switchbot_cloud.image import SwitchBotCloudImage
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import STATE_UNKNOWN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import configure_integration
|
||||
|
||||
@@ -81,3 +82,39 @@ async def test_async_image(
|
||||
)
|
||||
await image_entity.async_image()
|
||||
assert image_entity._image_content == mock_get.return_value
|
||||
|
||||
|
||||
async def test_image_state_is_timezone_aware(
|
||||
hass: HomeAssistant, mock_list_devices, mock_get_status
|
||||
) -> None:
|
||||
"""Test the image state carries a timezone.
|
||||
|
||||
The state is image_last_updated serialized, so a naive value would leave
|
||||
consumers of the state without an offset.
|
||||
"""
|
||||
mock_list_devices.return_value = [
|
||||
Device(
|
||||
version="V1.0",
|
||||
deviceId="ai-art-frame-id-1",
|
||||
deviceName="ai-art-frame-1",
|
||||
deviceType="AI Art Frame",
|
||||
hubDeviceId="test-hub-id",
|
||||
),
|
||||
]
|
||||
mock_get_status.side_effect = [
|
||||
{
|
||||
"deviceId": "B0E9FEA5D7F0",
|
||||
"deviceType": "AI Art Frame",
|
||||
"hubDeviceId": "B0E9FEA5D7F0",
|
||||
"battery": 0,
|
||||
"displayMode": 1,
|
||||
"imageUrl": "https://example.com/image.jpeg",
|
||||
"version": "V0.0-0.5",
|
||||
}
|
||||
]
|
||||
entry = await configure_integration(hass)
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
|
||||
state = hass.states.get("image.ai_art_frame_1_display")
|
||||
assert state.state != STATE_UNKNOWN
|
||||
assert dt_util.parse_datetime(state.state).tzinfo is not None
|
||||
|
||||
Reference in New Issue
Block a user