Skip to content

Commit

Permalink
Translation Support
Browse files Browse the repository at this point in the history
+ Added Translation Support and re-factored enumerations to translations
+ Added Finnish Language as First Translation
+ Minor Fixes and Changes
  • Loading branch information
veista committed Jul 29, 2023
1 parent 79cfc03 commit d4758f6
Show file tree
Hide file tree
Showing 14 changed files with 1,629 additions and 806 deletions.
3 changes: 1 addition & 2 deletions custom_components/nilan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True


async def async_migrate_entry(hass, config_entry: ConfigEntry):
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry):
"""Migrate old entry."""
_LOGGER.debug("Migrating from version %s", config_entry.version)

if config_entry.version == 1:

new = {**config_entry.data}
new.update({"com_type": "tcp"})

Expand Down
39 changes: 17 additions & 22 deletions custom_components/nilan/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
ATTRIBUTE_TO_BINARY_SENSORS = {
"get_compressor_state": [
Map(
"Compressor",
"compressor",
BinarySensorDeviceClass.RUNNING,
None,
None,
Expand All @@ -33,7 +33,7 @@
],
"get_smoke_alarm_state": [
Map(
"Smoke Alarm",
"smoke_alarm",
BinarySensorDeviceClass.SMOKE,
None,
None,
Expand All @@ -42,7 +42,7 @@
],
"get_defrost_state": [
Map(
"Defrost",
"defrost",
BinarySensorDeviceClass.RUNNING,
None,
"mdi:snowflake-melt",
Expand All @@ -51,7 +51,7 @@
],
"get_bypass_flap_state": [
Map(
"Bypass Flap",
"bypass_flap",
BinarySensorDeviceClass.OPENING,
None,
None,
Expand All @@ -60,7 +60,7 @@
],
"get_user_function_1_state": [
Map(
"User Selection 1",
"user_selection_1",
BinarySensorDeviceClass.RUNNING,
None,
"mdi:account",
Expand All @@ -69,7 +69,7 @@
],
"get_user_function_2_state": [
Map(
"User Selection 2",
"user_selection_2",
BinarySensorDeviceClass.RUNNING,
None,
"mdi:account",
Expand All @@ -78,7 +78,7 @@
],
"get_display_led_1_state": [
Map(
"Display LED 1",
"display_led_1",
BinarySensorDeviceClass.LIGHT,
None,
"mdi:led-on",
Expand All @@ -87,7 +87,7 @@
],
"get_display_led_2_state": [
Map(
"Display LED 2",
"display_led_2",
BinarySensorDeviceClass.LIGHT,
None,
"mdi:led-on",
Expand All @@ -96,7 +96,7 @@
],
"get_circulation_pump_state": [
Map(
"Circulation Pump",
"circulation_pump",
BinarySensorDeviceClass.RUNNING,
None,
"mdi:pump",
Expand All @@ -105,7 +105,7 @@
],
"get_heater_relay_1_state": [
Map(
"Heater Relay 1",
"heater_relay_1",
None,
None,
"mdi:electric-switch-closed",
Expand All @@ -114,7 +114,7 @@
],
"get_heater_relay_2_state": [
Map(
"Heater Relay 2",
"heater_relay_2",
None,
None,
"mdi:electric-switch-closed",
Expand All @@ -123,7 +123,7 @@
],
"get_heater_relay_3_state": [
Map(
"Heater Relay 3",
"heater_relay_3",
None,
None,
"mdi:electric-switch-closed",
Expand All @@ -133,9 +133,9 @@
}


async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_setup_entry(HomeAssistant, config_entry, async_add_entities):
"""Set up the sensor platform."""
device = hass.data[DOMAIN][config_entry.entry_id]
device = HomeAssistant.data[DOMAIN][config_entry.entry_id]
binary_sensors = []
for attribute in device.get_assigned("binary_sensor"):
if attribute in ATTRIBUTE_TO_BINARY_SENSORS:
Expand Down Expand Up @@ -175,19 +175,14 @@ def __init__(
self._attribute = attribute
self._device = device
self._available = True
self._attr_name = self._device.get_device_name + ": " + name
self._attr_device_class = device_class
self._attr_entity_category = entity_category
self._name = name
self._on_icon = on_icon
self._off_icon = off_icon

@property
def unique_id(self) -> str:
"""Return a unique ID."""
_name = self._device.get_device_name.lower().replace(" ", "_")
_unique_id = self._name.lower().replace(" ", "_")
return f"{_name}.{_unique_id}"
self._attr_has_entity_name = True
self._attr_translation_key = self._name
self._attr_unique_id = self._name

@property
def icon(self) -> str | None:
Expand Down
44 changes: 17 additions & 27 deletions custom_components/nilan/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
PRESET_COMFORT,
)

from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature

from .const import DOMAIN, SCAN_INTERVAL_TIME
from .__init__ import NilanEntity
Expand All @@ -31,19 +31,19 @@
}

PRESET_TO_HVAC = {
"Energy": 0,
PRESET_COMFORT: 1,
"Water": 2,
"energy": 0,
"comfort": 1,
"water": 2,
}

HVAC_TO_PRESET = {
0: "Energy",
1: PRESET_COMFORT,
2: "Water",
0: "energy",
1: "comfort",
2: "water",
}


async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_setup_entry(HomeAssistant, config_entry, async_add_entities):
"""Add climate entities for a config entry."""
supported_features = ClimateEntityFeature.FAN_MODE

Expand Down Expand Up @@ -73,7 +73,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):

entities = []
extra_status_attributes = False
device = hass.data[DOMAIN][config_entry.entry_id]
device = HomeAssistant.data[DOMAIN][config_entry.entry_id]
if all(attribute in device.get_attributes for attribute in hvac_basic_attributes):
if all(
attribute in device.get_attributes
Expand Down Expand Up @@ -102,9 +102,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class NilanClimate(NilanEntity, ClimateEntity):
"""Define a Nilan HVAC."""

def __init__(self, device, supported_featrures, extra_status_attributes):
def __init__(self, device, supported_featrures, extra_status_attributes) -> None:
"""Init the class."""
super().__init__(device)
self._attr_translation_key = "hvac"
self._attr_has_entity_name = True
self._attr_unique_id = "hvac"
self._hvac_on = False
self._attr_max_humidity = 45
self._attr_min_humidity = 15
Expand All @@ -117,13 +120,11 @@ def __init__(self, device, supported_featrures, extra_status_attributes):
HVACMode.AUTO,
HVACMode.OFF,
]
self._attr_preset_modes = ["Energy", PRESET_COMFORT, "Water"]
self._attr_preset_modes = ["energy", "comfort", "water"]
self._attr_fan_modes = ["0", "1", "2", "3", "4"]
self._attr_temperature_unit = TEMP_CELSIUS
self._attr_temperature_unit = UnitOfTemperature.CELSIUS
self._attr_supported_features = supported_featrures
self._extra_status_attributes = extra_status_attributes
self._attr_translation_key = "hvac"
self._attr_has_entity_name = True

async def async_set_fan_mode(self, fan_mode):
"""Set new target fan mode."""
Expand Down Expand Up @@ -169,6 +170,8 @@ async def async_update(self) -> None:

self._attr_fan_mode = str(await self._device.get_ventilation_step())
control_state = await self._device.get_control_state()
if control_state is None:
return

if self._attr_supported_features & ClimateEntityFeature.PRESET_MODE:
self._attr_preset_mode = HVAC_TO_PRESET.get(
Expand Down Expand Up @@ -203,16 +206,3 @@ async def async_update(self) -> None:
self._attr_hvac_mode = HVAC_MODE_TO_STATE.get(
await self._device.get_operation_mode()
)

@property
def name(self) -> str | None:
"""Return a name"""
_name = self._device.get_device_name
return f"{_name}: HVAC"

@property
def unique_id(self) -> str:
"""Return a unique ID."""
_name = self._device.get_device_name.lower().replace(" ", "_")
_unique_id = "hvac"
return f"{_name}.{_unique_id}"
Loading

0 comments on commit d4758f6

Please sign in to comment.