Finally, I made it!!
I made a copy from the templateswitch and then i edited the parts which were annoying me.
i named it Template2.
"""
Support for switches which integrates with other components.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.template/
"""
import logging
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchDevice
from homeassistant.const import (
ATTR_FRIENDLY_NAME, STATE_OFF, STATE_ON,
ATTR_ENTITY_ID, MATCH_ALL)
from homeassistant.exceptions import TemplateError
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.helpers.script import Script
from homeassistant.helpers import template
from homeassistant.helpers.event import track_state_change
from homeassistant.util import slugify
CONF_SWITCHES = 'switches'
ON_ACTION = 'turn_on'
OFF_ACTION = 'turn_off'
_LOGGER = logging.getLogger(__name__)
_VALID_STATES = [STATE_ON, STATE_OFF, 'true', 'false']
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Template switch."""
switches = []
if config.get(CONF_SWITCHES) is None:
_LOGGER.error("Missing configuration data for switch platform")
return False
for device, device_config in config[CONF_SWITCHES].items():
if device != slugify(device):
_LOGGER.error("Found invalid key for switch.template: %s. "
"Use %s instead", device, slugify(device))
continue
if not isinstance(device_config, dict):
_LOGGER.error("Missing configuration data for switch %s", device)
continue
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
on_action = device_config.get(ON_ACTION)
off_action = device_config.get(OFF_ACTION)
if on_action is None or off_action is None:
_LOGGER.error(
"Missing action for switch %s", device)
continue
entity_ids = device_config.get(ATTR_ENTITY_ID, MATCH_ALL)
switches.append(
SwitchTemplate(
hass,
device,
friendly_name,
on_action,
off_action,
entity_ids)
)
if not switches:
_LOGGER.error("No switches added")
return False
add_devices(switches)
return True
class SwitchTemplate(SwitchDevice):
"""Representation of a Template switch."""
# pylint: disable=too-many-arguments
def __init__(self, hass, device_id, friendly_name,
on_action, off_action, entity_ids):
"""Initialize the Template switch."""
self.hass = hass
self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id,
hass=hass)
self._name = friendly_name
#self._template = state_template
self._on_script = Script(hass, on_action)
self._off_script = Script(hass, off_action)
self._state = False
@property
def name(self):
"""Return the name of the switch."""
return self._name
@property
def is_on(self):
"""Return true if device is on."""
return self._state
@property
def should_poll(self):
"""No polling needed."""
return False
def turn_on(self, **kwargs):
"""Fire the on action."""
self._on_script.run()
self._state = True
self.update_ha_state()
def turn_off(self, **kwargs):
"""Fire the off action."""
self._off_script.run()
self._state = False
self.update_ha_state()
i made a sketch for the arduino which is actually a combination from some sketches which i found on several places.
#include <MySensor.h>
#include <SPI.h>
#include <RemoteTransmitter.h>
#define TRANSMITTERPIN 6
#define CHILD_ID_ELRO_SWITCH 4
#define CHILD_ID_KAKU_SWITCH 5
#define CHILD_ID_BLOKKER_SWITCH 6
#define CHILD_ID_ACTION_SWITCH 7
char Code1 = 'A';
int Code2 = 0;
boolean Code3;
char code[5]="A01t";
char oldCode[5]="A01t";
ElroTransmitter switchelroTransmitter = ElroTransmitter( TRANSMITTERPIN );
KaKuTransmitter switchkakuTransmitter = KaKuTransmitter( TRANSMITTERPIN );
BlokkerTransmitter switchblokkerTransmitter = BlokkerTransmitter( TRANSMITTERPIN );
ActionTransmitter switchactionTransmitter = ActionTransmitter( TRANSMITTERPIN );
MySensor gw;
MyMessage msg(0, V_LIGHT );
MyMessage msgCode(0, V_IR_SEND );
MyMessage msgCodeRec(0, V_IR_RECEIVE);
void setup() {
gw.begin(incomingMessage);
gw.sendSketchInfo("Afstandbediening", "1.0");
gw.present( CHILD_ID_ELRO_SWITCH, S_IR );
gw.present( CHILD_ID_KAKU_SWITCH, S_IR );
gw.present( CHILD_ID_BLOKKER_SWITCH, S_IR );
gw.present( CHILD_ID_ACTION_SWITCH, S_IR );
gw.send(msg.setSensor(CHILD_ID_ELRO_SWITCH).set(false));
gw.send(msg.setSensor(CHILD_ID_KAKU_SWITCH).set(false));
gw.send(msg.setSensor(CHILD_ID_BLOKKER_SWITCH).set(false));
gw.send(msg.setSensor(CHILD_ID_ACTION_SWITCH).set(false));
gw.send(msgCode.setSensor(CHILD_ID_ELRO_SWITCH).set(code));
gw.send(msgCode.setSensor(CHILD_ID_KAKU_SWITCH).set(code));
gw.send(msgCode.setSensor(CHILD_ID_BLOKKER_SWITCH).set(code));
gw.send(msgCode.setSensor(CHILD_ID_ACTION_SWITCH).set(code));
gw.send(msgCodeRec.setSensor(CHILD_ID_ELRO_SWITCH).set(code));
gw.send(msgCodeRec.setSensor(CHILD_ID_KAKU_SWITCH).set(code));
gw.send(msgCodeRec.setSensor(CHILD_ID_BLOKKER_SWITCH).set(code));
gw.send(msgCodeRec.setSensor(CHILD_ID_ACTION_SWITCH).set(code));
}
void loop() {
gw.process();
}
void incomingMessage(const MyMessage &message) {
Serial.println(message.sensor);
if (message.type == V_IR_SEND) {
String codestring = message.getString();
codestring.toCharArray(code, sizeof(code));
Code2=((code[1]-48)*10)+(code[2]-48);
if(code[3]==char('t')){
//Serial.println("switch on");
Code3=true;
} else {
//Serial.println("switch off");
Code3=false;
}
if (message.sensor==CHILD_ID_KAKU_SWITCH){
switchkakuTransmitter.sendSignal(code[0] , Code2 ,Code3 );
}
if (message.sensor==CHILD_ID_ELRO_SWITCH){
switchelroTransmitter.sendSignal(Code2 , code[0] ,Code3 );
}
if (message.sensor==CHILD_ID_BLOKKER_SWITCH){
switchblokkerTransmitter.sendSignal(Code2 ,Code3 );
}
if (message.sensor==CHILD_ID_ACTION_SWITCH){
switchactionTransmitter.sendSignal(Code2 , code[0] ,Code3 );
}
}
}
and then in de configuration.yaml
# switch 4 => ELRO, switch 5 => KAKU, switch 6 => Blokker, switch 7 Action
# ELROcode => (A/E) buttoncode, (01/31) dipswitchcode, (t or f) ON or OFF example: A15t
# KAKUcode => (A/P) groupcode(dipswitch), (01/16) buttoncode, (t or f) ON or OFF example: M03f
# Blokkercode => (A) to keep the code unified, (01/16) buttoncode, (t or f) ON or OFF example: A07t
# Actioncode => (A/D) buttoncode, (01/?) dipswitchcode, (t or f) ON or OFF example: A04f
switch:
platform: template2
switches:
diningroom:
friendly_name: diningroom
turn_on:
service: switch.mysensors_send_ir_code
entity_id: switch.remote_2_5 # switch 5 for KAKUswitches
data:
V_IR_SEND: 'A01t'
turn_off:
service: switch.mysensors_send_ir_code
entity_id: switch.remote_2_5 # switch 5 for KAKUswitches
data:
V_IR_SEND: 'A01f'
livingroom:
friendly_name: livingroom
turn_on:
service: switch.mysensors_send_ir_code
entity_id: switch.remote_2_4 # switch 4 for ELROswitches
data:
V_IR_SEND: 'A02t'
turn_off:
service: switch.mysensors_send_ir_code
entity_id: switch.remote_2_4 # switch 4 for ELROswitches
data:
V_IR_SEND: 'A02f'
bedroom:
friendly_name: bedroom
turn_on:
service: switch.mysensors_send_ir_code
entity_id: switch.remote_2_6 # switch 6 for BLOKKERswitches
data:
V_IR_SEND: 'A07t'
turn_off:
service: switch.mysensors_send_ir_code
entity_id: switch.remote_2_6 # switch 6 for BLOKKERswitches
data:
V_IR_SEND: 'A07f'
outside:
friendly_name: outside
turn_on:
service: switch.mysensors_send_ir_code
entity_id: switch.remote_2_7 # switch 7 for ACTIONswitches
data:
V_IR_SEND: 'A04t'
turn_off:
service: switch.mysensors_send_ir_code
entity_id: switch.remote_2_7 # switch 7 for ACTIONswitches
data:
V_IR_SEND: 'A04f'
in the part customize:
switch.remote_2_4:
hidden: true
switch.remote_2_5:
hidden: true
switch.remote_2_6:
hidden: true
switch.remote_2_7:
hidden: true
sensor.remote_2_4:
hidden: true
sensor.remote_2_5:
hidden: true
sensor.remote_2_6:
hidden: true
sensor.remote_2_7:
hidden: true
probably it can be made more perfect,
and it it al lot of code for the configuration, which would be easier to read if it was simplefied, but it works.
now i can go on implementing mor switches around the house (a have around 50 old ones lying around)
I can put an arduino in every room where it is needed and change all the hardware i want without reprogramming all the time.