Skip to content

Commit

Permalink
Working ->input_number
Browse files Browse the repository at this point in the history
  • Loading branch information
ThermIQ committed Dec 15, 2021
1 parent dd91229 commit 017f2dc
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 15 deletions.
65 changes: 51 additions & 14 deletions custom_components/thermiq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,19 @@
SERVICE_SELECT_PREVIOUS,
SERVICE_SET_OPTIONS,
)
from homeassistant.components.input_number import (
ATTR_VALUE as INP_ATTR_VALUE,
DOMAIN as INP_DOMAIN,
SERVICE_RELOAD,
SERVICE_SET_VALUE as INP_SERVICE_SET_VALUE,
)
from homeassistant.const import ATTR_ENTITY_ID # UNIT_PERCENTAGE,
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
ATTR_EDITABLE,
ATTR_ENTITY_ID,
ATTR_FRIENDLY_NAME,
ATTR_NAME,
CONF_HOST,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_HUMIDITY,
Expand Down Expand Up @@ -112,25 +122,13 @@ async def async_setup(hass, config):
hass.data[DOMAIN] = ThermIQ_MQTT(config[DOMAIN])
hass.states.async_set('thermiq_mqtt.time_str','Waiting on '+conf.data_topic)
hass.data[DOMAIN]._data['mqtt_counter']=0

for platform in THERMIQ_PLATFORMS:
_LOGGER.debug("platform:" + platform)
discovery.load_platform(hass, platform, DOMAIN, {}, config)

# Create reverse lookup dictionary (id_reg->reg_number)
id_reg= {}
for k,v in reg_id.items():
id_reg[v[0]]=k
hass.states.async_set('thermiq_mqtt.'+v[0],-1)
_LOGGER.debug("id_reg[%s] => %s",v[0],k)


# ### Setup our input helpers
CONFIG_INPUT_BOOLEAN.update(config.get(COMPONENT_INPUT_BOOLEAN, {}))
CONFIG_INPUT_DATETIME.update(config.get(COMPONENT_INPUT_DATETIME, {}))
CONFIG_INPUT_NUMBER.update(config.get(COMPONENT_INPUT_NUMBER, {}))
CONFIG_INPUT_TEXT.update(config.get(COMPONENT_INPUT_TEXT, {}))
CONFIG_TIMER.update(config.get(COMPONENT_TIMER, {}))
await create_input_datetime('aaa_date', True, False, icon='mdi:modern-house')

async def handle_home_assistant_started_event(event: Event):
await create_entities_and_automations(hass)
Expand All @@ -139,8 +137,45 @@ async def handle_automation_reload_event(event: Event):
await create_automations(hass)

hass.bus.async_listen(EVENT_HOMEASSISTANT_STARTED, handle_home_assistant_started_event)

hass.bus.async_listen(EVENT_AUTOMATION_RELOADED, handle_automation_reload_event)
# ###

# ### Load our sensors
for platform in THERMIQ_PLATFORMS:
_LOGGER.debug("platform:" + platform)
discovery.load_platform(hass, platform, DOMAIN, {}, config)

# Create reverse lookup dictionary (id_reg->reg_number)
id_reg= {}
for k,v in reg_id.items():
id_reg[v[0]]=k
hass.states.async_set('thermiq_mqtt.'+v[0],-1)
_LOGGER.debug("id_reg[%s] => %s",v[0],k)

# ### Lets create the inputs
icon_list={'time_input':'mdi:timer','sensor_input':'mdi:speedometer','temperature_input':'mdi:temperature-celsius'}
mode_list={'time_input':'slider','sensor_input':'box','temperature_input':'box'}
for key in reg_id:
if reg_id[key][1] in [
"temperature_input",
"time_input",
"sensor_input",

]:
device_id = key
if key in id_names:
friendly_name = id_names[key]
else:
friendly_name = None
input_reg = reg_id[key][0]
input_type = reg_id[key][1]
input_unit = reg_id[key][2]
input_min = reg_id[key][3]
input_max = reg_id[key][4]

await create_input_number('thermiq_'+key, friendly_name, input_min, input_max, 1, mode_list[input_type], input_unit, icon=icon_list[input_type])

# ###
@callback
def message_received(message):
Expand Down Expand Up @@ -168,7 +203,9 @@ def message_received(message):
hass.states.async_set("thermiq_mqtt."+id_reg[kstore],json_dict[k])
a=reg_id[id_reg[kstore]]
if (a[1]=='temperature_input') or (a[1]=='time_input') or (a[1]=='sensor_input') :
hass.states.async_set("input_number.thermiq_"+id_reg[kstore],json_dict[k])
cntxt= { INP_ATTR_VALUE: json_dict[k] , ATTR_ENTITY_ID: 'input_number.thermiq_'+id_reg[kstore]}
#_LOGGER.debug(cntxt)
hass.async_create_task(hass.services.async_call(INP_DOMAIN,INP_SERVICE_SET_VALUE, cntxt, blocking=False))
#if (a[1]=='select_input') :
#hass.states.async_set("input_select.thermiq_main_mode",'Auto');
_LOGGER.debug("[%s] [%s] [%s]", kstore, json_dict[k],dstore)
Expand Down
235 changes: 235 additions & 0 deletions custom_components/thermiq/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
from collections import OrderedDict
from datetime import timedelta
from logging import getLogger

from homeassistant.components.automation import async_setup as setup_automation, \
_async_process_config as add_automation, AutomationConfig
from homeassistant.components.input_boolean import async_setup as setup_input_boolean
from homeassistant.components.input_datetime import async_setup as setup_input_datetime
from homeassistant.components.input_number import async_setup as setup_input_number
from homeassistant.components.input_text import async_setup as setup_input_text
from homeassistant.components.timer import async_setup as setup_timer
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.entity_platform import async_get_platforms
from homeassistant.util import slugify

_LOGGER = getLogger(__name__)

COMPONENT_AUTOMATION = 'automation'
COMPONENT_INPUT_BOOLEAN = 'input_boolean'
COMPONENT_INPUT_DATETIME = 'input_datetime'
COMPONENT_INPUT_NUMBER = 'input_number'
COMPONENT_INPUT_TEXT = 'input_text'
COMPONENT_TIMER = 'timer'

CUSTOM_ENTITY_COMPONENTS = {}

SETUP_FUNCTION = {
COMPONENT_AUTOMATION: setup_automation
}

AUTOMATIONS = []

# all input_* entities in HA (config + custom)
CONFIG_INPUT_BOOLEAN = {}
CONFIG_INPUT_DATETIME = {}
CONFIG_INPUT_NUMBER = {}
CONFIG_INPUT_TEXT = {}
CONFIG_TIMER = {}

# save whether custom inputs were declared, so they can be added to HA
CUSTOM_INPUT_BOOLEAN = False
CUSTOM_INPUT_DATETIME = False
CUSTOM_INPUT_NUMBER = False
CUSTOM_INPUT_TEXT = False
CUSTOM_TIMER = False


def add_host(device_id: str, host: Entity):
HOSTS[device_id] = host


def get_host(device_id: str) -> Entity:
return HOSTS.get(device_id, None)


async def _get_platform(hass: HomeAssistant, domain: str):
platform_list = async_get_platforms(hass, domain)

for platform in platform_list:
if platform.domain == domain:
return platform

if domain not in CUSTOM_ENTITY_COMPONENTS:
await SETUP_FUNCTION[domain](hass, {})

CUSTOM_ENTITY_COMPONENTS[domain] = EntityComponent(
_LOGGER, domain, hass, timedelta(seconds=86400)
)

return CUSTOM_ENTITY_COMPONENTS[domain]


async def create_input_boolean(name: str, icon=None) -> str:
data = {
'name': name
}

if icon:
data['icon'] = icon

internal_name = slugify(name)

CONFIG_INPUT_BOOLEAN[internal_name] = data

global CUSTOM_INPUT_BOOLEAN
CUSTOM_INPUT_BOOLEAN = True

return 'input_boolean.{}'.format(internal_name)


async def create_input_datetime(name: str, has_date: bool, has_time: bool, initial=None,
icon=None) -> str:
data = {
'name': name,
'has_date': has_date,
'has_time': has_time
}

if initial:
data['initial'] = initial

if icon:
data['icon'] = icon

internal_name = slugify(name)

CONFIG_INPUT_DATETIME[internal_name] = OrderedDict(data)

global CUSTOM_INPUT_DATETIME
CUSTOM_INPUT_DATETIME = True

return 'input_datetime.{}'.format(internal_name)


async def create_input_number(name: str, friendly_name:str, _min: int, _max: int, step: int, mode: str,
unit_of_measurement: str, icon=None) -> str:
data = {
'name': friendly_name,
'min': _min,
'max': _max,
'step': step,
'mode': mode,
'friendly_name': friendly_name,
'unit_of_measurement': unit_of_measurement
}

if icon:
data['icon'] = icon

internal_name = slugify(name)

CONFIG_INPUT_NUMBER[internal_name] = data

global CUSTOM_INPUT_NUMBER
CUSTOM_INPUT_NUMBER = True

return 'input_number.{}'.format(internal_name)


async def create_input_text(name: str, _min=0, _max=100, initial=None,
pattern='', mode='text', icon=None) -> str:
data = {
'name': name,
'min': _min,
'max': _max,
'initial': initial,
'pattern': pattern,
'mode': mode
}

if icon:
data['icon'] = icon

internal_name = slugify(name)

CONFIG_INPUT_TEXT[internal_name] = data

global CUSTOM_INPUT_TEXT
CUSTOM_INPUT_TEXT = True

return 'input_text.{}'.format(internal_name)


async def create_timer(name: str, duration='00:00:00') -> str:
data = {
'name': name,
'duration': duration
}

internal_name = slugify(name)

CONFIG_TIMER[internal_name] = data

global CUSTOM_TIMER
CUSTOM_TIMER = True

return 'timer.{}'.format(internal_name)


async def create_entities_and_automations(hass: HomeAssistant):
if CUSTOM_INPUT_BOOLEAN:
for entity_id in list(
filter(lambda eid: COMPONENT_INPUT_BOOLEAN == eid.split('.')[0], hass.states.async_entity_ids())):
hass.states.async_remove(entity_id)

await setup_input_boolean(hass, {COMPONENT_INPUT_BOOLEAN: CONFIG_INPUT_BOOLEAN})

if CUSTOM_INPUT_DATETIME:
for entity_id in list(
filter(lambda eid: COMPONENT_INPUT_DATETIME == eid.split('.')[0], hass.states.async_entity_ids())):
hass.states.async_remove(entity_id)

await setup_input_datetime(hass, {COMPONENT_INPUT_DATETIME: CONFIG_INPUT_DATETIME})

if CUSTOM_INPUT_NUMBER:
for entity_id in list(
filter(lambda eid: COMPONENT_INPUT_NUMBER == eid.split('.')[0], hass.states.async_entity_ids())):
hass.states.async_remove(entity_id)

await setup_input_number(hass, {COMPONENT_INPUT_NUMBER: CONFIG_INPUT_NUMBER})

if CUSTOM_INPUT_TEXT:
for entity_id in list(
filter(lambda eid: COMPONENT_INPUT_TEXT == eid.split('.')[0], hass.states.async_entity_ids())):
hass.states.async_remove(entity_id)

await setup_input_text(hass, {COMPONENT_INPUT_TEXT: CONFIG_INPUT_TEXT})

if CUSTOM_TIMER:
for entity_id in list(
filter(lambda eid: COMPONENT_TIMER == eid.split('.')[0], hass.states.async_entity_ids())):
hass.states.async_remove(entity_id)

await setup_timer(hass, {COMPONENT_TIMER: CONFIG_TIMER})

await create_automations(hass)


async def create_automation(data: dict):
automation = AutomationConfig(data)
raw_config = dict(data)
automation.raw_config = raw_config
AUTOMATIONS.append(automation)


async def create_automations(hass: HomeAssistant):
platform = await _get_platform(hass, "automation")

data = {
'automation': AUTOMATIONS
}

await add_automation(hass, OrderedDict(data), platform)
2 changes: 1 addition & 1 deletion custom_components/thermiq/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from homeassistant.helpers.entity import Entity, async_generate_entity_id

from . import DOMAIN as THERMIQ_DOMAIN
from . import id_names as id_names
from . import (
FIELD_BITMASK,
FIELD_MAXVALUE,
Expand All @@ -22,6 +21,7 @@
id_names,
id_units,
reg_id,
id_names
)

_LOGGER = logging.getLogger(__name__)
Expand Down

0 comments on commit 017f2dc

Please sign in to comment.