Skip to content

Commit

Permalink
Format with black
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamish Findlay committed Nov 5, 2023
1 parent 341ba9f commit 32a73c8
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 299 deletions.
205 changes: 97 additions & 108 deletions custom_components/battery_sim/__init__.py

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions custom_components/battery_sim/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,4 @@ def should_poll(self):
return False

async def async_press(self):
dispatcher_send(
self.hass,
f"{self._device_name}-{MESSAGE_TYPE_GENERAL}"
)
dispatcher_send(self.hass, f"{self._device_name}-{MESSAGE_TYPE_GENERAL}")
223 changes: 127 additions & 96 deletions custom_components/battery_sim/config_flow.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion custom_components/battery_sim/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
CONF_BATTERY_SIZE: 5.0,
CONF_BATTERY_EFFICIENCY: 0.81,
CONF_BATTERY_MAX_CHARGE_RATE: 2.2,
CONF_BATTERY_MAX_DISCHARGE_RATE: 1.7
CONF_BATTERY_MAX_DISCHARGE_RATE: 1.7,
},
"Custom": {},
}
38 changes: 22 additions & 16 deletions custom_components/battery_sim/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
CONF_SECOND_ENERGY_EXPORT_TARIFF,
IMPORT,
EXPORT,
SIMULATED_SENSOR
SIMULATED_SENSOR,
)

"""For backwards compatability with old configs"""


def generate_input_list(config):
tariff_type: str = TARIFF_SENSOR
if TARIFF_TYPE in config:
Expand All @@ -38,29 +40,33 @@ def generate_input_list(config):
SENSOR_ID: config[CONF_IMPORT_SENSOR],
SENSOR_TYPE: IMPORT,
SIMULATED_SENSOR: GRID_IMPORT_SIM,
TARIFF_TYPE: tariff_type
TARIFF_TYPE: tariff_type,
},
{
SENSOR_ID: config[CONF_EXPORT_SENSOR],
SENSOR_TYPE: EXPORT,
SIMULATED_SENSOR: GRID_EXPORT_SIM,
TARIFF_TYPE: tariff_type
TARIFF_TYPE: tariff_type,
},
]
if len(config.get(CONF_SECOND_IMPORT_SENSOR, "")) > 6:
inputs.append({
SENSOR_ID: config[CONF_SECOND_IMPORT_SENSOR],
SENSOR_TYPE: IMPORT,
SIMULATED_SENSOR: GRID_SECOND_IMPORT_SIM,
TARIFF_TYPE: tariff_type
})
inputs.append(
{
SENSOR_ID: config[CONF_SECOND_IMPORT_SENSOR],
SENSOR_TYPE: IMPORT,
SIMULATED_SENSOR: GRID_SECOND_IMPORT_SIM,
TARIFF_TYPE: tariff_type,
}
)
if len(config.get(CONF_SECOND_EXPORT_SENSOR, "")) > 6:
inputs.append({
SENSOR_ID: config[CONF_SECOND_EXPORT_SENSOR],
SENSOR_TYPE: EXPORT,
SIMULATED_SENSOR: GRID_SECOND_EXPORT_SIM,
TARIFF_TYPE: tariff_type
})
inputs.append(
{
SENSOR_ID: config[CONF_SECOND_EXPORT_SENSOR],
SENSOR_TYPE: EXPORT,
SIMULATED_SENSOR: GRID_SECOND_EXPORT_SIM,
TARIFF_TYPE: tariff_type,
}
)

"""Default sensor entities for backwards compatibility"""
if CONF_ENERGY_IMPORT_TARIFF in config:
Expand All @@ -75,4 +81,4 @@ def generate_input_list(config):
inputs[2][tariff_type] = config[CONF_SECOND_ENERGY_IMPORT_TARIFF]
if CONF_SECOND_ENERGY_EXPORT_TARIFF in config:
inputs[3][tariff_type] = config[CONF_SECOND_ENERGY_EXPORT_TARIFF]
return inputs
return inputs
8 changes: 4 additions & 4 deletions custom_components/battery_sim/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
CHARGE_ONLY,
DISCHARGE_ONLY,
DEFAULT_MODE,
ICON_FULL
ICON_FULL,
)

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass, config_entry, async_add_entities):
handle = hass.data[DOMAIN][config_entry.entry_id] # Get Handler

Expand Down Expand Up @@ -56,7 +57,7 @@ def __init__(self, handle):
PAUSE_BATTERY,
FORCE_DISCHARGE,
CHARGE_ONLY,
DISCHARGE_ONLY
DISCHARGE_ONLY,
]
self._current_option = DEFAULT_MODE

Expand Down Expand Up @@ -85,7 +86,7 @@ def icon(self):
def current_option(self):
"""Return the state of the sensor."""
return self._current_option

@property
def options(self):
return self._options
Expand All @@ -98,4 +99,3 @@ async def async_select_option(self, option: str):
self.handle._switches[option] = True
self.schedule_update_ha_state(True)
return True

106 changes: 37 additions & 69 deletions custom_components/battery_sim/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import logging

import homeassistant.util.dt as dt_util
from homeassistant.helpers.dispatcher import (
dispatcher_send,
async_dispatcher_connect
)
from homeassistant.helpers.dispatcher import dispatcher_send, async_dispatcher_connect

from homeassistant.components.sensor import (
SensorDeviceClass,
Expand All @@ -19,7 +16,7 @@
STATE_UNAVAILABLE,
STATE_UNKNOWN,
UnitOfPower,
UnitOfEnergy
UnitOfEnergy,
)

from .const import (
Expand Down Expand Up @@ -56,7 +53,7 @@
MODE_EMPTY,
BATTERY_CYCLES,
MESSAGE_TYPE_BATTERY_UPDATE,
SENSOR_ID
SENSOR_ID,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -114,18 +111,12 @@ async def define_sensors(hass, handle):
)
sensors.append(
DisplayOnlySensor(
handle,
CHARGING_RATE,
SensorDeviceClass.POWER,
UnitOfPower.KILO_WATT
handle, CHARGING_RATE, SensorDeviceClass.POWER, UnitOfPower.KILO_WATT
)
)
sensors.append(
DisplayOnlySensor(
handle,
DISCHARGING_RATE,
SensorDeviceClass.POWER,
UnitOfPower.KILO_WATT
handle, DISCHARGING_RATE, SensorDeviceClass.POWER, UnitOfPower.KILO_WATT
)
)
for input in handle._inputs:
Expand All @@ -138,13 +129,7 @@ async def define_sensors(hass, handle):
)
)

sensors.append(
DisplayOnlySensor(
handle,
BATTERY_CYCLES,
None,
None)
)
sensors.append(DisplayOnlySensor(handle, BATTERY_CYCLES, None, None))

sensors.append(
DisplayOnlySensor(
Expand Down Expand Up @@ -207,9 +192,7 @@ async def async_added_to_hass(self):
try:
self._handle._sensors[self._sensor_type] = float(state.state)
self._last_reset = dt_util.as_utc(
dt_util.parse_datetime(
state.attributes.get(ATTR_LAST_RESET)
)
dt_util.parse_datetime(state.attributes.get(ATTR_LAST_RESET))
)
self._available = True
await self.async_update_ha_state(True)
Expand Down Expand Up @@ -244,10 +227,7 @@ def unique_id(self):

@property
def device_info(self):
return {
"name": self._device_name,
"identifiers": {(DOMAIN, self._device_name)}
}
return {"name": self._device_name, "identifiers": {(DOMAIN, self._device_name)}}

@property
def native_value(self):
Expand Down Expand Up @@ -277,14 +257,15 @@ def extra_state_attributes(self):
"""Return the state attributes of the sensor."""
state_attr = {}
for input in self._handle._inputs:
if self._sensor_type != input[SIMULATED_SENSOR]: break
if input[SENSOR_TYPE] == EXPORT: break
if self._sensor_type != input[SIMULATED_SENSOR]:
break
if input[SENSOR_TYPE] == EXPORT:
break
parent_sensor = input[SENSOR_ID]
if (
self.hass.states.get(parent_sensor) is None
or self.hass.states.get(parent_sensor).state
in [STATE_UNAVAILABLE, STATE_UNKNOWN]
): break
if self.hass.states.get(parent_sensor) is None or self.hass.states.get(
parent_sensor
).state in [STATE_UNAVAILABLE, STATE_UNKNOWN]:
break
real_world_value = float(self.hass.states.get(parent_sensor).state)
simulated_value = self._handle._sensors[self._sensor_type]
if real_world_value == 0:
Expand All @@ -297,8 +278,7 @@ def extra_state_attributes(self):
state_attr = {PERCENTAGE_ENERGY_IMPORT_SAVED: 0}
else:
percentage_value_saved = (
100 * (real_world_value - simulated_value)
/ real_world_value
100 * (real_world_value - simulated_value) / real_world_value
)
state_attr = {
PERCENTAGE_ENERGY_IMPORT_SAVED: round(
Expand All @@ -314,7 +294,11 @@ def icon(self):
@property
def state(self):
"""Return the state of the sensor."""
if self._sensor_type in [ATTR_MONEY_SAVED, ATTR_MONEY_SAVED_EXPORT, ATTR_MONEY_SAVED_IMPORT]:
if self._sensor_type in [
ATTR_MONEY_SAVED,
ATTR_MONEY_SAVED_EXPORT,
ATTR_MONEY_SAVED_IMPORT,
]:
return round(float(self._handle._sensors[self._sensor_type]), 2)
else:
return round(float(self._handle._sensors[self._sensor_type]), 3)
Expand Down Expand Up @@ -352,7 +336,9 @@ async def async_added_to_hass(self):

state = await self.async_get_last_state()
if state:
self.handle._charge_state = float(state.state)
self.handle._charge_state = min(
float(state.state), self.handle._battery_size
)
if ATTR_DATE_RECORDING_STARTED in state.attributes:
self.handle._date_recording_started = state.attributes[
ATTR_DATE_RECORDING_STARTED
Expand All @@ -363,9 +349,7 @@ async def async_update_state():
await self.async_update_ha_state(True)

async_dispatcher_connect(
self.hass,
f"{self._name}-{MESSAGE_TYPE_BATTERY_UPDATE}",
async_update_state
self.hass, f"{self._name}-{MESSAGE_TYPE_BATTERY_UPDATE}", async_update_state
)

@property
Expand Down Expand Up @@ -417,30 +401,20 @@ def extra_state_attributes(self):
for input in self.handle._inputs:
sensor_list = f"{sensor_list}, {input[SENSOR_ID]}"
return {
ATTR_STATUS:
self.handle._sensors[BATTERY_MODE],
ATTR_CHARGE_PERCENTAGE:
int(self.handle._charge_percentage),
ATTR_DATE_RECORDING_STARTED:
self.handle._date_recording_started,
CONF_BATTERY_SIZE:
self.handle._battery_size,
CONF_BATTERY_EFFICIENCY:
float(self.handle._battery_efficiency),
CONF_BATTERY_MAX_DISCHARGE_RATE:
float(self.handle._max_discharge_rate),
CONF_BATTERY_MAX_CHARGE_RATE:
float(self.handle._max_charge_rate),
ATTR_SOURCE_ID:
sensor_list
ATTR_STATUS: self.handle._sensors[BATTERY_MODE],
ATTR_CHARGE_PERCENTAGE: int(self.handle._charge_percentage),
ATTR_DATE_RECORDING_STARTED: self.handle._date_recording_started,
CONF_BATTERY_SIZE: self.handle._battery_size,
CONF_BATTERY_EFFICIENCY: float(self.handle._battery_efficiency),
CONF_BATTERY_MAX_DISCHARGE_RATE: float(self.handle._max_discharge_rate),
CONF_BATTERY_MAX_CHARGE_RATE: float(self.handle._max_charge_rate),
ATTR_SOURCE_ID: sensor_list,
}

@property
def icon(self):
"""Return the icon to use in the frontend."""
if self.handle._sensors[BATTERY_MODE] in [
MODE_CHARGING, MODE_FORCE_CHARGING
]:
if self.handle._sensors[BATTERY_MODE] in [MODE_CHARGING, MODE_FORCE_CHARGING]:
return ICON_CHARGING
if self.handle._sensors[BATTERY_MODE] == MODE_FULL:
return ICON_FULL
Expand Down Expand Up @@ -492,10 +466,7 @@ def unique_id(self):

@property
def device_info(self):
return {
"name": self._device_name,
"identifiers": {(DOMAIN, self._device_name)}
}
return {"name": self._device_name, "identifiers": {(DOMAIN, self._device_name)}}

@property
def native_value(self):
Expand All @@ -515,10 +486,7 @@ def extra_state_attributes(self):
@property
def icon(self):
"""Return the icon to use in the frontend."""
if self.handle._sensors[BATTERY_MODE] in [
MODE_CHARGING,
MODE_FORCE_CHARGING
]:
if self.handle._sensors[BATTERY_MODE] in [MODE_CHARGING, MODE_FORCE_CHARGING]:
return ICON_CHARGING
if self.handle._sensors[BATTERY_MODE] == MODE_FULL:
return ICON_FULL
Expand Down
2 changes: 1 addition & 1 deletion custom_components/battery_sim/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
PAUSE_BATTERY,
FORCE_DISCHARGE,
CHARGE_ONLY,
DISCHARGE_ONLY
DISCHARGE_ONLY,
)

_LOGGER = logging.getLogger(__name__)
Expand Down

0 comments on commit 32a73c8

Please sign in to comment.