Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for showing the time in a different time zone."""
2 
3 from __future__ import annotations
4 
5 from datetime import tzinfo
6 
7 import voluptuous as vol
8 
10  PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
11  SensorEntity,
12 )
13 from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
14 from homeassistant.const import CONF_NAME, CONF_TIME_ZONE
15 from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
17 from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
20 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
21 import homeassistant.util.dt as dt_util
22 
23 from .const import CONF_TIME_FORMAT, DEFAULT_NAME, DEFAULT_TIME_STR_FORMAT, DOMAIN
24 
25 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
26  {
27  vol.Required(CONF_TIME_ZONE): cv.time_zone,
28  vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
29  vol.Optional(CONF_TIME_FORMAT, default=DEFAULT_TIME_STR_FORMAT): cv.string,
30  }
31 )
32 
33 
35  hass: HomeAssistant,
36  config: ConfigType,
37  async_add_entities: AddEntitiesCallback,
38  discovery_info: DiscoveryInfoType | None = None,
39 ) -> None:
40  """Set up the World clock sensor."""
41  hass.async_create_task(
42  hass.config_entries.flow.async_init(
43  DOMAIN,
44  context={"source": SOURCE_IMPORT},
45  data=config,
46  )
47  )
48 
50  hass,
51  HOMEASSISTANT_DOMAIN,
52  f"deprecated_yaml_{DOMAIN}",
53  breaks_in_ha_version="2025.2.0",
54  is_fixable=False,
55  issue_domain=DOMAIN,
56  severity=IssueSeverity.WARNING,
57  translation_key="deprecated_yaml",
58  translation_placeholders={
59  "domain": DOMAIN,
60  "integration_title": "Worldclock",
61  },
62  )
63 
64 
66  hass: HomeAssistant,
67  entry: ConfigEntry,
68  async_add_entities: AddEntitiesCallback,
69 ) -> None:
70  """Set up the World clock sensor entry."""
71  time_zone = await dt_util.async_get_time_zone(entry.options[CONF_TIME_ZONE])
73  [
75  time_zone,
76  entry.options[CONF_NAME],
77  entry.options[CONF_TIME_FORMAT],
78  entry.entry_id,
79  )
80  ],
81  True,
82  )
83 
84 
86  """Representation of a World clock sensor."""
87 
88  _attr_icon = "mdi:clock"
89  _attr_has_entity_name = True
90  _attr_name = None
91 
92  def __init__(
93  self, time_zone: tzinfo | None, name: str, time_format: str, unique_id: str
94  ) -> None:
95  """Initialize the sensor."""
96  self._time_zone_time_zone = time_zone
97  self._time_format_time_format = time_format
98  self._attr_unique_id_attr_unique_id = unique_id
99  self._attr_device_info_attr_device_info = DeviceInfo(
100  identifiers={(DOMAIN, unique_id)},
101  name=name,
102  entry_type=DeviceEntryType.SERVICE,
103  manufacturer="Worldclock",
104  )
105 
106  async def async_update(self) -> None:
107  """Get the time and updates the states."""
108  self._attr_native_value_attr_native_value = dt_util.now(time_zone=self._time_zone_time_zone).strftime(
109  self._time_format_time_format
110  )
None __init__(self, tzinfo|None time_zone, str name, str time_format, str unique_id)
Definition: sensor.py:94
None async_create_issue(HomeAssistant hass, str entry_id)
Definition: repairs.py:69
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:39
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:69