1 """Helper and wrapper classes for Gree module."""
3 from __future__
import annotations
5 from datetime
import datetime, timedelta
9 from greeclimate.device
import Device, DeviceInfo
10 from greeclimate.discovery
import Discovery, Listener
11 from greeclimate.exceptions
import DeviceNotBoundError, DeviceTimeoutError
12 from greeclimate.network
import Response
23 DISPATCH_DEVICE_DISCOVERED,
29 _LOGGER = logging.getLogger(__name__)
33 """Manages polling for state changes from the device."""
35 def __init__(self, hass: HomeAssistant, device: Device) ->
None:
36 """Initialize the data update coordinator."""
37 DataUpdateCoordinator.__init__(
41 name=f
"{DOMAIN}-{device.device_info.name}",
42 update_interval=
timedelta(seconds=UPDATE_INTERVAL),
54 """Handle device state updates."""
55 _LOGGER.debug(
"Device state updated: %s",
json_dumps(args))
61 """Update the state of the device."""
63 "Updating device state: %s, error count: %d", self.
namename, self.
_error_count_error_count
66 await self.
devicedevice.update_state()
67 except DeviceNotBoundError
as error:
69 f
"Device {self.name} is unavailable, device is not bound."
71 except DeviceTimeoutError
as error:
77 "Device %s is unavailable: %s", self.
namename, self.
devicedevice.device_info
80 f
"Device {self.name} is unavailable, could not send update request"
94 "Device %s is unresponsive for %s seconds",
100 f
"Device {self.name} is unresponsive for too long and now unavailable"
103 return self.
devicedevice.raw_properties
106 """Send state updates to the physical device."""
109 except DeviceTimeoutError:
111 "Timeout send state update to: %s (%s)",
113 self.
devicedevice.device_info,
118 """Discovery event handler for gree devices."""
121 """Initialize discovery service."""
126 self.
discoverydiscovery.add_listener(self)
128 hass.data[DOMAIN].setdefault(COORDINATORS, [])
131 """Handle new device found on the network."""
133 device =
Device(device_info)
136 except DeviceNotBoundError:
137 _LOGGER.error(
"Unable to bind to gree device: %s", device_info)
138 except DeviceTimeoutError:
139 _LOGGER.error(
"Timeout trying to bind to gree device: %s", device_info)
142 "Adding Gree device %s at %s:%i",
143 device.device_info.name,
144 device.device_info.ip,
145 device.device_info.port,
148 self.
hasshass.data[DOMAIN][COORDINATORS].append(coordo)
149 await coordo.async_refresh()
154 """Handle updates in device information, update if ip has changed."""
155 for coordinator
in self.
hasshass.data[DOMAIN][COORDINATORS]:
156 if coordinator.device.device_info.mac == device_info.mac:
157 coordinator.device.device_info.ip = device_info.ip
158 await coordinator.async_refresh()
dict[str, Any] _async_update_data(self)
def push_state_update(self)
None device_state_updated(self, *Any args)
None __init__(self, HomeAssistant hass, Device device)
None device_update(self, DeviceInfo device_info)
None __init__(self, HomeAssistant hass)
None device_found(self, DeviceInfo device_info)
None update_interval(self, timedelta|None value)
timedelta|None update_interval(self)
None async_set_updated_data(self, _DataT data)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)