1
1
# Copyright (c) 2019-2021, Andrey "Limych" Khrolenok <andrey@khrolenok.ru>
2
2
# Creative Commons BY-NC-SA 4.0 International Public License
3
3
# (see LICENSE.md or https://creativecommons.org/licenses/by-nc-sa/4.0/)
4
- """The Car Wash binary sensor.
4
+ """
5
+ The Car Wash binary sensor.
5
6
6
7
For more details about this platform, please refer to the documentation at
7
8
https://github.com/Limych/ha-car_wash/
8
9
"""
9
10
10
- from collections .abc import Callable
11
- from datetime import datetime
12
11
import logging
12
+ from datetime import datetime
13
13
14
14
import voluptuous as vol
15
-
16
15
from homeassistant .components .binary_sensor import BinarySensorEntity
17
16
from homeassistant .components .weather import (
18
17
ATTR_FORECAST_CONDITION ,
21
20
ATTR_FORECAST_TEMP_LOW ,
22
21
ATTR_FORECAST_TIME ,
23
22
ATTR_WEATHER_TEMPERATURE ,
24
- DOMAIN as WEATHER_DOMAIN ,
25
23
SERVICE_GET_FORECASTS ,
26
24
WeatherEntityFeature ,
27
25
)
26
+ from homeassistant .components .weather import (
27
+ DOMAIN as WEATHER_DOMAIN ,
28
+ )
28
29
from homeassistant .const import (
29
30
ATTR_SUPPORTED_FEATURES ,
30
31
CONF_ENTITY_ID ,
34
35
EVENT_HOMEASSISTANT_START ,
35
36
UnitOfTemperature ,
36
37
)
37
- from homeassistant .core import Event , EventStateChangedData , HomeAssistant , callback
38
+ from homeassistant .core import Event , HomeAssistant , callback
38
39
from homeassistant .exceptions import HomeAssistantError
39
40
from homeassistant .helpers import config_validation as cv
41
+ from homeassistant .helpers .entity_platform import AddEntitiesCallback
40
42
from homeassistant .helpers .event import async_track_state_change_event
41
- from homeassistant .helpers .typing import ConfigType
43
+ from homeassistant .helpers .typing import ConfigType , DiscoveryInfoType
42
44
from homeassistant .util import dt as dt_util
43
45
from homeassistant .util .unit_conversion import TemperatureConverter
44
46
67
69
68
70
# pylint: disable=unused-argument
69
71
async def async_setup_platform (
70
- hass : HomeAssistant ,
72
+ hass : HomeAssistant , # noqa: ARG001
71
73
config : ConfigType ,
72
- async_add_entities : Callable ,
73
- discovery_info = None ,
74
- ):
74
+ async_add_entities : AddEntitiesCallback ,
75
+ discovery_info : DiscoveryInfoType = None , # noqa: ARG001
76
+ ) -> None :
75
77
"""Set up the Car Wash sensor."""
76
78
# Print startup message
77
79
_LOGGER .info (STARTUP_MESSAGE )
@@ -97,7 +99,7 @@ def __init__(
97
99
friendly_name : str ,
98
100
weather_entity : str ,
99
101
days : int ,
100
- ):
102
+ ) -> None :
101
103
"""Initialize the sensor."""
102
104
self ._weather_entity = weather_entity
103
105
self ._days = days
@@ -124,19 +126,19 @@ async def async_added_to_hass(self) -> None:
124
126
125
127
# pylint: disable=unused-argument
126
128
@callback
127
- def sensor_state_listener (event : Event [ EventStateChangedData ] ) -> None :
129
+ def sensor_state_listener (event : Event ) -> None : # noqa: ARG001
128
130
"""Handle device state changes."""
129
- self .async_schedule_update_ha_state (True )
131
+ self .async_schedule_update_ha_state (force_refresh = True )
130
132
131
133
# pylint: disable=unused-argument
132
134
@callback
133
- def sensor_startup (event ) -> None :
135
+ def sensor_startup (event : Event ) -> None : # noqa: ARG001
134
136
"""Update template on startup."""
135
137
async_track_state_change_event (
136
138
self .hass , [self ._weather_entity ], sensor_state_listener
137
139
)
138
140
139
- self .async_schedule_update_ha_state (True )
141
+ self .async_schedule_update_ha_state (force_refresh = True )
140
142
141
143
self .hass .bus .async_listen_once (EVENT_HOMEASSISTANT_START , sensor_startup )
142
144
@@ -150,13 +152,12 @@ def _temp2c(temperature: float | None, temperature_unit: str) -> float | None:
150
152
return temperature
151
153
152
154
# pylint: disable=too-many-branches,too-many-statements
153
- async def async_update (self ):
155
+ async def async_update (self ) -> None : # noqa: PLR0912, PLR0915
154
156
"""Update the sensor state."""
155
157
wstate = self .hass .states .get (self ._weather_entity )
156
158
if wstate is None :
157
- raise HomeAssistantError (
158
- f"Unable to find an entity called { self ._weather_entity } "
159
- )
159
+ msg = f"Unable to find an entity called { self ._weather_entity } "
160
+ raise HomeAssistantError (msg )
160
161
161
162
tmpu = self .hass .config .units .temperature_unit
162
163
temp = wstate .attributes .get (ATTR_WEATHER_TEMPERATURE )
@@ -170,7 +171,8 @@ async def async_update(self):
170
171
elif (wfeatures & WeatherEntityFeature .FORECAST_HOURLY ) != 0 :
171
172
forecast_type = "hourly"
172
173
else :
173
- raise HomeAssistantError ("Weather entity doesn't support any forecast" )
174
+ msg = "Weather entity doesn't support any forecast"
175
+ raise HomeAssistantError (msg )
174
176
175
177
try :
176
178
forecast = await self .hass .services .async_call (
@@ -185,9 +187,8 @@ async def async_update(self):
185
187
)
186
188
except HomeAssistantError as ex :
187
189
self ._attr_is_on = None
188
- raise HomeAssistantError (
189
- "Can't get forecast data! Are you sure it's the weather provider?"
190
- ) from ex
190
+ msg = "Can't get forecast data! Are you sure it's the weather provider?"
191
+ raise HomeAssistantError (msg ) from ex
191
192
192
193
_LOGGER .debug ("Current temperature %s, condition '%s'" , temp , cond )
193
194
@@ -201,7 +202,8 @@ async def async_update(self):
201
202
today = dt_util .start_of_local_day ()
202
203
cur_date = today .strftime ("%F" )
203
204
stop_date = datetime .fromtimestamp (
204
- today .timestamp () + 86400 * (self ._days + 1 )
205
+ today .timestamp () + 86400 * (self ._days + 1 ),
206
+ tz = dt_util .DEFAULT_TIME_ZONE ,
205
207
).strftime ("%F" )
206
208
207
209
_LOGGER .debug ("Inspect weather forecast from now till %s" , stop_date )
0 commit comments