Understanding what is wrong with custom test platform

I am trying to make a custom test platform, and trying to understand what is going on by diving into the code, I have read the minimal docs for creating a platform at dev.home-assistant.io and am currently stuck

I am using this code:

import logging

import voluptuous as vol

from homeassistant.components.switch import SwitchDevice
from random import randint

_LOGGER  = logging.getLogger(__name__)


def setup_platform(hass, config, add_entities, discovery_info=None):
    """Setup the software light"""
    ports = config.get('lights')
    
    _LOGGER.debug("port list: %s type %s", ports, type(ports))
    lights = []
    for light_num, light_name in ports.items():
        _LOGGER.debug("ports.items num: %s name: %s type: %s", light_num, light_name, type(light_name))
        lights.append(SoftwareSwitch(light_name))
    _LOGGER.debug("list of lights %s", lights)
    add_entities(lights, True)


class SoftwareSwitch(SwitchDevice):
    """Software Light"""

    def __init__(self, name):
        _LOGGER.debug("initialized %s", name)
        _LOGGER.debug("initialized type %s", type(name))

        self._name = name
        self._state = True

    def name(self):
        return self._name

    def should_poll(self):
        return True
    def is_on(self):
        return self._state

    def turn_on(self):
        self._state = True
        self.schedule_update_ha_state()

    def turn_off(self):
        self._state = False
        self.schedule_update_ha_state()
    
    def update(self):
        if (randint(0,5) == 1):
            self._state = not self._state
        _LOGGER.debug("updating Software Light name: %s %s", self._name, self._state)

And this code inside my configuration file:

switch:
  - platform: software_switch
    lights:
      0: light one
      1: light two

I have tried writing it as a light component as well, and have been referencing the various platforms inside switch and light (Yeelight, gpio_sensor, (in binary_sensor), demo.py, abode.py eufy.py etc)

I am getting this error:

2018-09-13 21:47:18 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py", line 332, in _async_add_entity
    self.domain, suggested_object_id)
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_registry.py", line 108, in async_generate_entity_id
    '{}.{}'.format(domain, slugify(suggested_object_id)),
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/util/__init__.py", line 46, in slugify
    text = normalize('NFKD', text)
TypeError: normalize() argument 2 must be str, not method

I can modify line 322/323 (of homeassistant/helpers/entity_platform.py ) from
suggested_object_id =
suggested_object_id or entity.name or DEVICE_DEFAULT_NAME

to just suggested_object_id = "no_name"

Then it creates a different error on line 191-193 in homeassistant/helpers/entity_component.py in the lambda function that it cannot sort (method() >method())

I have tried my best to find out what is wrong, but cannot seem to figure it out spent the last few hours dealing with this don’t know what to do to solve it.

Thanks!

Also homeassistant version is 0.77.3

After using homeassistant/components/switch/demo.py and removing things till they were the same found out the issue

There needs to be an @property on the name method

so

@property
def name(self):
return self._name

Instead of what I have up there without the @property

Although if anyone has a good rule of thumb on when things need to be labled @property I would love to know!

https://developers.home-assistant.io/docs/en/entity_index.html

@awarecan Thanks I see that I missed/forgot about that thanks!