diff --git a/custom_components/ohme/__init__.py b/custom_components/ohme/__init__.py index 9b4899b..1bedc3b 100644 --- a/custom_components/ohme/__init__.py +++ b/custom_components/ohme/__init__.py @@ -30,11 +30,12 @@ async def async_setup_entry(hass, entry): return False await async_setup_dependencies(hass, config) - + hass.data[DOMAIN][DATA_COORDINATOR] = OhmeUpdateCoordinator(hass=hass) await hass.data[DOMAIN][DATA_COORDINATOR].async_config_entry_first_refresh() - hass.data[DOMAIN][DATA_STATISTICS_COORDINATOR] = OhmeStatisticsUpdateCoordinator(hass=hass) + hass.data[DOMAIN][DATA_STATISTICS_COORDINATOR] = OhmeStatisticsUpdateCoordinator( + hass=hass) await hass.data[DOMAIN][DATA_STATISTICS_COORDINATOR].async_config_entry_first_refresh() # Create tasks for each entity type @@ -50,6 +51,7 @@ async def async_setup_entry(hass, entry): return True + async def async_unload_entry(hass, entry): """Unload a config entry.""" diff --git a/custom_components/ohme/client/__init__.py b/custom_components/ohme/client/__init__.py index 24d1b30..40af91b 100644 --- a/custom_components/ohme/client/__init__.py +++ b/custom_components/ohme/client/__init__.py @@ -100,12 +100,12 @@ async def async_resume_charge(self): """Resume a paused charge""" result = await self._post_request(f"https://api.ohme.io/v1/chargeSessions/{self._serial}/resume", skip_json=True) return bool(result) - + async def async_max_charge(self): """Enable max charge""" result = await self._put_request(f"https://api.ohme.io/v1/chargeSessions/{self._serial}/rule?maxCharge=true") return bool(result) - + async def async_stop_max_charge(self): """Stop max charge. This is more complicated than starting one as we need to give more parameters.""" @@ -116,7 +116,7 @@ async def async_get_charge_sessions(self, is_retry=False): """Try to fetch charge sessions endpoint. If we get a non 200 response, refresh auth token and try again""" resp = await self._get_request('https://api.ohme.io/v1/chargeSessions') - + if not resp: return False @@ -139,7 +139,7 @@ async def async_update_device_info(self, is_retry=False): sw_version=device['firmwareVersionLabel'], serial_number=device['id'] ) - + self._user_id = resp['user']['id'] self._serial = device['id'] self._device_info = info @@ -150,7 +150,8 @@ def _last_second_of_month_timestamp(self): """Get the last second of this month.""" dt = datetime.today() dt = dt.replace(day=1) + timedelta(days=32) - dt = dt.replace(day=1, hour=0, minute=0, second=0, microsecond=0) - timedelta(seconds=1) + dt = dt.replace(day=1, hour=0, minute=0, second=0, + microsecond=0) - timedelta(seconds=1) return int(dt.timestamp()*1e3) async def async_get_charge_statistics(self): @@ -168,4 +169,3 @@ def get_device_info(self): def get_unique_id(self, name): return f"ohme_{self._serial}_{name}" - diff --git a/custom_components/ohme/sensor.py b/custom_components/ohme/sensor.py index c180029..b80e120 100644 --- a/custom_components/ohme/sensor.py +++ b/custom_components/ohme/sensor.py @@ -23,7 +23,8 @@ async def async_setup_entry( coordinator = hass.data[DOMAIN][DATA_COORDINATOR] stats_coordinator = hass.data[DOMAIN][DATA_STATISTICS_COORDINATOR] - sensors = [PowerDrawSensor(coordinator, hass, client), EnergyUsageSensor(stats_coordinator, hass, client)] + sensors = [PowerDrawSensor(coordinator, hass, client), EnergyUsageSensor( + stats_coordinator, hass, client)] async_add_entities(sensors, update_before_add=True) @@ -49,7 +50,8 @@ def __init__( self.entity_id = generate_entity_id( "sensor.{}", "ohme_power_draw", hass=hass) - self._attr_device_info = hass.data[DOMAIN][DATA_CLIENT].get_device_info() + self._attr_device_info = hass.data[DOMAIN][DATA_CLIENT].get_device_info( + ) @property def unique_id(self) -> str: @@ -90,7 +92,8 @@ def __init__( self.entity_id = generate_entity_id( "sensor.{}", "ohme_accumulative_energy", hass=hass) - self._attr_device_info = hass.data[DOMAIN][DATA_CLIENT].get_device_info() + self._attr_device_info = hass.data[DOMAIN][DATA_CLIENT].get_device_info( + ) @property def unique_id(self) -> str: diff --git a/custom_components/ohme/switch.py b/custom_components/ohme/switch.py index 3533f4b..b5c3107 100644 --- a/custom_components/ohme/switch.py +++ b/custom_components/ohme/switch.py @@ -84,10 +84,11 @@ async def async_turn_on(self): async def async_turn_off(self): """Turn off the switch.""" await self._client.async_resume_charge() - + await asyncio.sleep(1) await self.coordinator.async_refresh() + class OhmeMaxCharge(CoordinatorEntity[OhmeUpdateCoordinator], SwitchEntity): """Switch for pausing a charge.""" _attr_name = "Ohme Max Charge" @@ -122,7 +123,8 @@ def _handle_coordinator_update(self) -> None: if self.coordinator.data is None: self._attr_is_on = False else: - self._attr_is_on = bool(self.coordinator.data["mode"] == "MAX_CHARGE") + self._attr_is_on = bool( + self.coordinator.data["mode"] == "MAX_CHARGE") self._last_updated = utcnow()